package cn.openea.perpartner.wxpay.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.Objects;

/**
 * @author Administrator
 */
@Configuration
public class RabbitConfig implements EnvironmentAware {
    private Environment env;

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }

    @Bean
    public Exchange businessExchange() {

        return ExchangeBuilder.directExchange(env.getProperty("rabbit.business.exchange")).build();
    }

    /**
     * 主题交换机
     * @return
     */
   /* @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(env.getProperty("rabbit.business.exchange"));
    }*/

    @Bean
    public Queue businessQueue() {

        return new Queue(Objects.requireNonNull(env.getProperty("rabbit.business.queue")), true, false, false);
    }
   /* @Bean
    public Binding topicBinding() {
        return BindingBuilder.bind(this.businessQueue())
                .to(this.topicExchange())
                .with("wxpay.*");
    }*/

    /**
     * 延时绑定区,业务绑定区
     * @return 延时绑定区
     */
    @Bean
    public Binding businessBind() {
        return BindingBuilder.bind(this.businessQueue())
                .to(this.businessExchange())
                .with(env.getProperty("rabbit.business.routing_key"))
                .noargs();
    }

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


}
